home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13832 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: erich.triumf.ca!bennett
  2. From: bennett@erich.triumf.ca (P.Bennett)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help:what is wrong this code?
  5. Date: 10 Apr 1996 07:39 PST
  6. Organization: TRIUMF: Tri-University Meson Facility
  7. Distribution: world
  8. Message-ID: <10APR199607391465@erich.triumf.ca>
  9. References: <4k3p3q$n76@brahms.udel.edu> <4k4hi3$5hm@sparcserver.lrz-muenchen.de> <Pine.ULT.3.92.960409210416.383A-100000@henson.cc.wwu.edu>
  10. NNTP-Posting-Host: ftp.triumf.ca
  11. News-Software: VAX/VMS VNEWS 1.50    
  12.  
  13. In article <Pine.ULT.3.92.960409210416.383A-100000@henson.cc.wwu.edu>, Norma Mathews <n9341884@henson.cc.wwu.edu> writes...
  14. >On 6 Apr 1996, Kurt Watzka wrote:
  15. >> yuehong@brahms.udel.edu (Yue-hong Zheng) writes:
  16. >> >Why it give me 0.0000?
  17. >>
  18. >> >#include <stdio.h>
  19. >> >main () {
  20. >> >double a=9.008;
  21. >> >printf("%f\n",sqrt(a));
  22. >> >return 0;
  23. >> >}
  24. >your printf specifer needs to be "%lf", not "%f", since a is a double
  25.  
  26. No!
  27.  
  28. %f is correct for _printf()_, since with variable argument lists, floats are
  29. promoted to doubles.  printf() can never see a float, so it does not need any
  30. way to distinguish between float and double. (not so with scanf(), which does
  31. use %f and %lf.)
  32.  
  33. (anyway, we aren't printing a, we are printing sqrt(a), so the type of a is
  34. irrelevant.)
  35.  
  36. The problem apparently is that the original poster failed to #include <math.h>,
  37. so the compiler will assume sqrt() returns an int.
  38.  
  39. Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight
  40. Internet: bennett@triumf.ca         | of one another only when one can be
  41. Packet: ve7cei@ve7kit.#vanc.bc.ca   | observed visually from the other
  42. TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
  43. GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
  44. or: ftp://ftp-i2.informatik.rwth-aachen.de/pub/arnd/GPS/peter/index.html
  45.  
  46.  
  47.  
  48.